package com.paranoid.shiro04.shiro.realms;

import com.paranoid.shiro04.entity.Perms;
import com.paranoid.shiro04.entity.User;
import com.paranoid.shiro04.service.UserService;
import com.paranoid.shiro04.utils.ApplicationContextUtils;
import com.paranoid.shiro04.utils.MyByteSource;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;


/**
 * 自定义realm
 */

public class CustomerRealm extends AuthorizingRealm {
    /**
     * 授权方法
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {

        //获取用户认证身份信息
        String primaryPrincipal = (String) principal.getPrimaryPrincipal();
        System.out.println("调用授权验证" + primaryPrincipal);
        UserService userService = (UserService) ApplicationContextUtils.getBean("userService");
        User user = userService.findRolesByUserName(primaryPrincipal);
//        System.out.println(user);
//        System.out.println(user.getRoles());
        if (!CollectionUtils.isEmpty(user.getRoles())){
            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
            user.getRoles().forEach(role -> {
                simpleAuthorizationInfo.addRole(role.getName());

                /*
                根据查出的role集合去遍历roleId  并利用roleId 查询到权限的集合
                判断权限的集合是否为空   不为空将对应的权限名称通过addStringPermission进行权限的添加
                */
                List<Perms> perms = userService.findPermsByRoleId(role.getId());
                if (!CollectionUtils.isEmpty(perms)){
                    perms.forEach(perm -> {
                        simpleAuthorizationInfo.addStringPermission(perm.getName());
                    });
                }
            });
        return simpleAuthorizationInfo;
        }

        //根据主身份信息获取角色信息以及权限信息
        /*
        if ("jay".equals(primaryPrincipal)) {
            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
            simpleAuthorizationInfo.addRole("admin");
            simpleAuthorizationInfo.addRole("user");
            simpleAuthorizationInfo.addStringPermission("user:select:*");
            simpleAuthorizationInfo.addStringPermission("user:update:*");
            return simpleAuthorizationInfo;
        }*/

        return null;
    }


    /**
     * 认证方法
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //根据身份信息
        String principal = (String) token.getPrincipal();
        //SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo();

        //在工厂中获取service对象
        UserService userService = (UserService) ApplicationContextUtils.getBean("userService");

        User user = userService.findByUserName(principal);

        if (!ObjectUtils.isEmpty(user)) {
            return new SimpleAuthenticationInfo(
                    user.getUsername(),
                    user.getPassword(),
                    new MyByteSource(user.getSalt()),
                    this.getName()
            );
        }
        /*if ("jay".equals(principal)){
         return new SimpleAuthenticationInfo(
                                             principal,
                                             "123",
                                             this.getName());
        }*/
        return null;
    }
}
